package dongtaibiancheng;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.Properties;

import javax.swing.JOptionPane;

public class Commandsystem {
	private Properties props = new Properties();

	public Commandsystem() throws Exception {
		// 加载资源文件，相对路径，根目录"/"指的是包所在的目录
		InputStream in = Commandsystem.class
				.getResourceAsStream("/command.properties");
		props.load(in);// 从流中读出键值对，直接加入到哈希表
		in.close();
	}

	public void launch() throws Exception {
		while (true) {
			String cmd = JOptionPane.showInputDialog(null, "请输入指令：");
			if ("exit".equalsIgnoreCase(cmd)) {
				break;
			}
			String s;
			if (cmd == null)
				continue;
			else
				s = props.getProperty(cmd);
			while (s == null) {
				cmd = JOptionPane.showInputDialog(null, "不存在的指令，请重新输入：");
				if ("exit".equalsIgnoreCase(cmd)) {
					System.out.println("bye-bye!");
					System.exit(0);
				}
				if (cmd == null)
					continue;
				else
					s = props.getProperty(cmd);
			}
			String[] arr = s.split(",");
			// 类对象
			Class c = Class.forName(arr[0]);
			// 获得方法
			Method m = c.getMethod(arr[1], null);
			// 创建实例
			Object obj = c.newInstance();
			// 通过实例执行方法
			String returnVal = (String) m.invoke(obj, null);
			System.out.println(returnVal);
		}
		System.out.println("bye-bye");
	}

	public static void main(String[] args) throws Exception {
		Commandsystem cs = new Commandsystem();
		cs.launch();
	}

}
